namespace Smarts.Internal.Test.Core
{
    using NUnit.Framework;
    using Smarts.Core;
    using TestDataDataSetTableAdapters;

    /// <summary>
    /// Tests for the StringSimilarity class
    /// </summary>
    [TestFixture]
    public class StringComparisonTests
    {
        #region Test configuration
        /// <summary>
        /// Table containing the strings to be compared in these tests
        /// </summary>
        private TestDataDataSet.StringComparisonDataTable stringsToCompare;

        /// <summary>
        /// Loads the test data for use during tests
        /// </summary>
        [TestFixtureSetUp]
        public void LoadTestData()
        {
            this.stringsToCompare = new TestDataDataSet.StringComparisonDataTable();
            StringComparisonTableAdapter tda = new StringComparisonTableAdapter();
            tda.Fill(this.stringsToCompare);
        }
        #endregion

        #region StringSimilarity tests

        /// <summary>
        /// Tests that StringSimilarity is able to distinguish equal strings
        /// </summary>
        [Test]
        [Category("StringSimilarity")]
        public void StringComparisonEqual()
        {
            foreach (TestDataDataSet.StringComparisonRow stringComparisonRow in this.stringsToCompare)
            {
                StringSimilarity comparer = new StringSimilarity(stringComparisonRow.String1, stringComparisonRow.String2);
                Assert.That(
                    comparer.Equal,
                    Is.EqualTo(stringComparisonRow.Equal),
                    "Comparing\nString1 [{0}]\nString2 [{1}]",
                    stringComparisonRow.String1,
                    stringComparisonRow.String2);
            }
        }

        /// <summary>
        /// Tests that StringSimilarity is able to detect strings
        /// that differ only by case
        /// </summary>
        [Test]
        [Category("StringSimilarity")]
        public void StringComparisonDifferByCaseOnly()
        {
            foreach (TestDataDataSet.StringComparisonRow stringComparisonRow in this.stringsToCompare)
            {
                StringSimilarity comparer = new StringSimilarity(stringComparisonRow.String1, stringComparisonRow.String2);
                Assert.That(
                    comparer.DifferByCaseOnly,
                    Is.EqualTo(stringComparisonRow.DifferByCaseOnly),
                    "Comparing\nString1 [{0}]\nString2 [{1}]",
                    stringComparisonRow.String1,
                    stringComparisonRow.String2);
            }
        }

        /// <summary>
        /// Tests that StringSimilarity is able to detect strings
        /// that differ only by leading and trailing whitespace
        /// </summary>
        [Test]
        [Category("StringSimilarity")]
        public void StringComparisonDifferByWhiteSpaceOnly()
        {
            foreach (TestDataDataSet.StringComparisonRow stringComparisonRow in this.stringsToCompare)
            {
                StringSimilarity comparer = new StringSimilarity(stringComparisonRow.String1, stringComparisonRow.String2);
                Assert.That(
                    comparer.DifferByWhiteSpaceOnly,
                    Is.EqualTo(stringComparisonRow.DifferByWhiteSpaceOnly),
                    "Comparing\nString1 [{0}]\nString2 [{1}]",
                    stringComparisonRow.String1,
                    stringComparisonRow.String2);
            }
        }

        /// <summary>
        /// Test that StringSimilarity can detect whether
        /// two strings contain each other
        /// </summary>
        [Test]
        [Category("StringSimilarity")]
        public void StringComparisonString1ContainsString2()
        {
            foreach (TestDataDataSet.StringComparisonRow stringComparisonRow in this.stringsToCompare)
            {
                StringSimilarity comparer = new StringSimilarity(stringComparisonRow.String1, stringComparisonRow.String2);
                Assert.That(
                    comparer.String1ContainsString2,
                    Is.EqualTo(stringComparisonRow.String1ContainsString2),
                    "Comparing\nString1 [{0}]\nString2 [",
                    stringComparisonRow.String1,
                    stringComparisonRow.String2);
            }
        }

        /// <summary>
        /// Tests that StringSimilarity can detect strings
        /// contain each other (in the opposite direction)
        /// </summary>
        [Test]
        [Category("StringSimilarity")]
        public void StringComparisonString2ContainsString1()
        {
            foreach (TestDataDataSet.StringComparisonRow stringComparisonRow in this.stringsToCompare)
            {
                StringSimilarity comparer = new StringSimilarity(stringComparisonRow.String1, stringComparisonRow.String2);
                Assert.That(
                    comparer.String2ContainsString1,
                    Is.EqualTo(stringComparisonRow.String2ContainsString1),
                    "Comparing\nString1 [{0}]\nString2 [{1}]",
                    stringComparisonRow.String1,
                    stringComparisonRow.String2);
            }
        }

        #endregion
    }
}